package com.digitalchina.lxnsy.activity.old;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessPlan;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.ProductStandardVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.viewholder.SelectProductionHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/8/21 18:56
 * 修改备注：
 * ================================================
 */
public class SelectProductionActivity extends BaseActivity {

    @Bind(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    @Bind(R.id.tv_stage)
    TextView tvStage;
    @Bind(R.id.layout_data)
    LinearLayout layoutData;
    @Bind(R.id.layout_empty)
    LinearLayout layoutEmpty;

    private List<ProductStandardVO> voList = new ArrayList<>();
    private Map<String, String> map = new HashMap<>();

    private RecyclerArrayAdapter adapter;
    private String cropType = ""; // 蔬菜 vegetable 果树 fruitTree
    private String suitCropCode = ""; // 农作物信息CODE
    private String suitGrowWayCode = ""; // 栽培方式CODE（蔬菜时必填）

    private int index = -1; // 标记选了哪个标砖

    private OptionsPickerView pvStage;

    String productStandardId = ""; // 生产标准Id
    String farmOperationId = ""; // 农事操作阶段ID

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_select_production);
        setTitle("选择生产标准");
    }

    @Override
    public void initMember() {

        map = (Map<String, String>) getIntent().getSerializableExtra("map");

        if (map.get("planTypeCode").equals("3001002")) {
            // 蔬菜
            cropType = "vegetable";
            suitGrowWayCode = map.get("plantTypeCode");
        } else
            cropType = "fruitTree";


        suitCropCode = map.get("cropTypeCode");


        //水平切换
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        recyclerView.setAdapter(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new SelectProductionHolder(parent);
            }
        });

        // 初始化阶段
        pvStage = new OptionsPickerView.Builder(SelectProductionActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {


                String select = voList.get(index).getPhaseMsgInfos().get(options1).getPhaseName() + "-" + voList.get(index).getPhaseMsgInfos().get(options1).getFarmOperation().get(option2).getFarmName();
                farmOperationId = voList.get(index).getPhaseMsgInfos().get(options1).getFarmOperation().get(option2).getId();
                tvStage.setText(select);
            }
        })
                .setSubCalSize(16)//确定取消按钮大小
                .setLineSpacingMultiplier(1.5f) //设置两横线之间的间隔倍数（范围：1.2 - 2.0倍 文字高度）
                .setDividerColor(getResources().getColor(R.color.colorFontLightGrayB))//设置分割线的颜色
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(18)//设置滚轮文字大小
                .setSelectOptions(0)  //设置默认选中项
                .build();

        BusinessPlan.queryProductStandard(this, cropType, suitCropCode, suitGrowWayCode, mHandler);

    }

    @Override
    public void addListener() {
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                index = position;

                for (int i = 0; i < voList.size(); i++) {
                    voList.get(i).setChecked(position == i);
                }
                adapter.notifyDataSetChanged();

                productStandardId = voList.get(position).getId();
                List<List<ProductStandardVO.PhaseMsgInfosBean.FarmOperationBean>> list = new ArrayList<>();
                for (int i = 0; i < voList.get(position).getPhaseMsgInfos().size(); i++) {
                    if (voList.get(position).getPhaseMsgInfos().get(i).getFarmOperation() != null)
                        list.add(voList.get(position).getPhaseMsgInfos().get(i).getFarmOperation());
                }
                if (list.size() > 0)
                    pvStage.setPicker(voList.get(position).getPhaseMsgInfos(), list);
                else
                    showToast("当前标准不完善，请选择其他标准。");
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {
                    case MSG.MSG_QUERY_PRODUCT_STANDARD_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_QUERY_PRODUCT_STANDARD_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_ADD_PLAN_SUCCESS:
                        showToast("新增成功");
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case MSG.MSG_ADD_PLAN_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

        voList = FastJsonUtil.getListBean(object.toString(), "productStandardList", ProductStandardVO.class);

        adapter.addAll(voList);

        if (voList == null || voList.size() <= 0) {
            layoutEmpty.setVisibility(View.VISIBLE);
            layoutData.setVisibility(View.GONE);
        }else{
            layoutEmpty.setVisibility(View.GONE);
            layoutData.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.layout_stage, R.id.bt_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_stage:
                if (index == -1)
                    showToast("请选择种植标准");
                else
                    pvStage.show();
                break;
            case R.id.bt_save:

                if (StringUtil.isStrEmpty(productStandardId)) {
                    showToast("请选择生产标准");
                    return;
                }

                if (StringUtil.isStrEmpty(farmOperationId)) {
                    showToast("请选择农事操作阶段");
                    return;
                }

                map.put("productStandardId", productStandardId);
                map.put("farmOperationId", farmOperationId);

                BusinessPlan.addPlan(SelectProductionActivity.this, map, mHandler);
                break;
        }
    }
}
